package com.hsmpay.mobile.action.bank;

import com.hsmpay.common.util.city.CityUtil;
import com.hsmpay.mobile.action.base.MobileBaseAction;
import com.hsmpay.mobile.action.user.UserAction;
import com.hsmpay.mobile.pojo.bank.Bank;
import com.hsmpay.mobile.pojo.bank.BankBranches;
import com.hsmpay.mobile.service.bank.BankBranchesService;
import com.hsmpay.mobile.service.bank.BankService;
import com.hsmpay.utils.JSONUtil;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 合作银行 action
 *
 * @author 颜铃璋
 * @date 2012-12-14
 */
@Controller("bankAction")
@Scope("prototype")
public class BankAction extends MobileBaseAction {
    private static final long serialVersionUID = 1L;
    private static Logger log = Logger.getLogger(UserAction.class);
    @Resource(name = "bankService")
    private BankService<Bank, Long> bankService;
    @Resource(name = "bankBranchesService")
    private BankBranchesService<BankBranches, Long> bankBranchesService;

    @SuppressWarnings("unused")
    private String openingBank;//开户行
    private Long bankId;//开户行
    private Integer bBankProvince;//支行所在省
    private Integer bBankCity;//支行所在市
    private String bBankName;

    /**
     * 查询银行列表
     *
     * @return
     * @throws Exception
     */
    public String searchBankList() throws Exception {
        log.debug("--- searchBankList() start ---");

        try {
            Map<String, Object> result = new LinkedHashMap<String, Object>();
            //Bank param = new Bank();
            //param.setStatus(1);
            List<Bank> bankList = bankService.searchCleanBankList();//查询合作银行
            List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();//结果集
            Map<String, Object> element = null;

            for (Bank bank : bankList) {//遍历 银行列表
                element = new LinkedHashMap<String, Object>();
                element.put("id", bank.getId());
                element.put("name", bank.getName());
                resultList.add(element);
            }

            //--------------成功信息
            result.put("reqCode", "0000");
            result.put("reqMsg", "获取银行列表成功！");
            result.put("bankList", resultList);
            sendAppMsg(result);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            log.debug("--- searchBankList() end ---");
        }
        return null;
    }

    /**
     * http://localhost/wanjxMobile/bank/bankAction_searchBranchesBankList.action?bankId=23&bBankProvince=10&bBankCity=3060
     * 查询支行列表
     *
     * @param openingBank   开户行
     * @param bBankProvince 支行所在省
     * @param bBankCity     支行所在市
     * @return
     * @throws Exception
     */
    public String searchBranchesBankList() throws Exception {
        log.debug("--- searchBranchesBankList() start ---");
        JSONObject js = JSONObject.fromObject(getRequest().getAttribute(FC_DATA));

        if (js.containsKey("bankId"))
            bankId = Long.parseLong(js.getString("bankId"));
        if (js.containsKey("bBankProvince"))
            bBankProvince = Integer.parseInt(js.getString("bBankProvince"));
        if (js.containsKey("bBankCity"))
            bBankCity = Integer.parseInt(js.getString("bBankCity"));
        if (js.containsKey("bBankName"))
            bBankName = js.getString("bBankName");
        if (js.containsKey("start"))
            start = Integer.parseInt(js.getString("start"));
        if (js.containsKey("stop"))
            stop = Integer.parseInt(js.getString("stop"));

        log.debug("bankId = " + bankId);
        log.debug("bBankProvince = " + bBankProvince);
        log.debug("bBankCity = " + bBankCity);
        log.debug("bBankName = " + bBankName);
        if (null == bankId) {
            log.warn("查询支行列表时, 传入参数bankId为空！");
            sendAppErrorMsg("6001", "bankId");
            return null;
        }
        if (null == bBankProvince) {
            log.warn("查询支行列表时, 传入参数bBankProvince为空！");
            sendAppErrorMsg("6001", "bBankProvince");
            return null;
        }
        if (null == bBankCity) {
            log.warn("查询支行列表时, 传入参数bBankCity为空！");
            sendAppErrorMsg("6001", "bBankCity");
            return null;
        }

        if (null == start) {
            start = 1;
        }
        if (null == stop) {
            stop = 20;
        }
        try {
            Map<String, Object> result = new LinkedHashMap<String, Object>();
            List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();//结果集
            Map<String, Object> element = null;
            /*if(-1 == bBankCity){
				sendAjaxResponse(result);
				return null;
			}*/

            int nextCode = CityUtil.getNextCityCode(bBankCity.toString());
            BankBranches bankBranches = new BankBranches();
            bankBranches.setBankId(bankId);
            bankBranches.setProvince(bBankProvince);
            bankBranches.setCity(bBankCity);
            bankBranches.setOtherParam(" CITY BETWEEN " + bBankCity + " AND " + nextCode);
            bankBranches.setName(bBankName);
            bankBranches.setStart(start);
            bankBranches.setStop(stop);
            //bankBranches.setCity(null);
            List<BankBranches> bankBranchesList = bankBranchesService.searchBankBranchesList(bankBranches);

            for (BankBranches bankB : bankBranchesList) {//遍历 银行列表
                element = new LinkedHashMap<String, Object>();
                element.put("id", bankB.getId());
                element.put("name", bankB.getName());
                resultList.add(element);
            }
            result.put("reqCode", "0000");
            result.put("reqMsg", "获取银行列表成功！");
            result.put("bBankList", resultList);
            sendAppMsg(result);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            log.debug("--- searchBranchesBankList() end ---");
        }
        return null;
    }
    
    /**
     * 公众号      获取支行信息
     * @return
     * @throws Exception
     */
    public String selectBranchesBankList() throws Exception {
    	
    	 String bankId_ = getRequest().getParameter("bankId");
    	 String bBankProvince_ = getRequest().getParameter("bBankProvince");
    	 String bBankCity_ = getRequest().getParameter("bBankCity");
    	 
    	if(StringUtils.isNotEmpty(bankId_)){
    		bankId = Long.parseLong(bankId_);
    	}
    	if(StringUtils.isNotEmpty(bBankProvince_)){
    		bBankProvince = Integer.parseInt(bBankProvince_);
    	}
    	if(StringUtils.isNotEmpty(bBankCity_)){
    		bBankCity = Integer.parseInt(bBankCity_);
    	}
        if (null == bankId) {
            log.warn("查询支行列表时, 传入参数bankId为空！");
            sendAppErrorMsg("6001", "bankId");
            return null;
        }
        if (null == bBankProvince) {
            log.warn("查询支行列表时, 传入参数bBankProvince为空！");
            sendAppErrorMsg("6001", "bBankProvince");
            return null;
        }
        if (null == bBankCity) {
            log.warn("查询支行列表时, 传入参数bBankCity为空！");
            sendAppErrorMsg("6001", "bBankCity");
            return null;
        }

        if (null == start) {
            start = 1;
        }
        if (null == stop) {
            stop = 100;
        }
        try {
            Map<String, Object> result = new LinkedHashMap<String, Object>();
            List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();//结果集
            Map<String, Object> element = null;
            int nextCode = CityUtil.getNextCityCode(bBankCity.toString());
            BankBranches bankBranches = new BankBranches();
            bankBranches.setBankId(bankId);
            bankBranches.setProvince(bBankProvince);
            bankBranches.setCity(bBankCity);
            bankBranches.setOtherParam(" CITY BETWEEN " + bBankCity + " AND " + nextCode);
            bankBranches.setName(bBankName);
            bankBranches.setStart(start);
            bankBranches.setStop(stop);
            //bankBranches.setCity(null);
            List<BankBranches> bankBranchesList = bankBranchesService.searchBankBranchesList(bankBranches);
            for (BankBranches bankB : bankBranchesList) {//遍历 银行列表
                element = new LinkedHashMap<String, Object>();
                element.put("id", bankB.getId());
                element.put("name", bankB.getName());
                resultList.add(element);
            }
            sendWeChatMsg(JSONUtil.parseToString(resultList));
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            log.debug("--- searchBranchesBankList() end ---");
        }
        return null;
    }
  
}
